package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * TODO: 增加描述
 *
 * @author li.xh
 * @version 1.0.0
 * @date 2019-12-13 10:37
 * @copyright wonhigh.cn
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize    = queryPageBean.getPageSize();
        String  queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        roleDao.deleteById(id);
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //添加user
        roleDao.addRole(role);

        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                //添加user和permission的关系
                map.put("role_id", role.getId());
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }

        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                //添加user和permission的关系
                map.put("role_id", role.getId());
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public Role findById(Integer id) {
        //回显基本信息
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //删除与permission外键
        permissionDao.deleteForeignKey(role.getId());
        //添加与permission外键
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                //添加user和permission的关系
                map.put("role_id", role.getId());
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
        //删除与menu外键
        menuDao.deleteForeignKey(role.getId());
        //添加与menu外键
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                //添加user和permission的关系
                map.put("role_id", role.getId());
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
}
